/*
 * Copyright (C), 2002-2017, 南京苏宁软件技术有限公司
 * FileName: DistributeStatusServiceImpl.java
 * Author:   10070706
 * Date:     2017年1月9日 下午3:30:34
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.service.impl.mainpushnew;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.suning.sawp.constants.MainPushConstants;
import com.suning.sawp.intf.dao.mainpushnew.AreaTaskDao;
import com.suning.sawp.intf.dao.mainpushnew.DistributeStatusDao;
import com.suning.sawp.intf.mainpushnew.AreaTaskService;
import com.suning.sawp.intf.mainpushnew.DistributeStatusService;
import com.suning.sawp.po.mainpushnew.AreaModelRel;
import com.suning.sawp.po.mainpushnew.AreaTask;

/**
 * 功能简述: 大区任务分配状态<br>
 *
 * @author 10070706 陈辉
 */
@Service
public class DistributeStatusServiceImpl implements DistributeStatusService {

    @Resource(name = "areaTaskService")
    private AreaTaskService self;

    @Autowired
    private DistributeStatusDao distributeStatusDao;
    @Autowired
    private AreaTaskDao areaTaskDao;

    @Override
    public void modifyAreaTaskStatus4NewModel(Long hqTaskId) {
        List<AreaTask> areaTaskList = areaTaskDao.queryAreaTaskByHqTaskId(hqTaskId);
        List<AreaTask> areaTaskStatusList = new ArrayList<AreaTask>();
        AreaTask areaTaskStatus = null;
        for (AreaTask task : areaTaskList) {
            switch (task.getDistStatus()) {
                // 未分配
                case 0:
                    // 不做修改
                    break;
                // 已分配
                case 1:
                    areaTaskStatus = new AreaTask();
                    areaTaskStatus.setId(task.getId());
                    areaTaskStatus.setDistStatus(MainPushConstants.AREA_TASK_STATUS_REDISTRIBUTE);
                    areaTaskStatusList.add(areaTaskStatus);
                    break;
                // 重新分配
                case 2:
                    // 不做修改
                    break;
                // 分配中
                case 3:
                    areaTaskStatus = new AreaTask();
                    areaTaskStatus.setId(task.getId());
                    areaTaskStatus.setDistStatus(MainPushConstants.AREA_TASK_STATUS_REDISTRIBUTE);
                    areaTaskStatusList.add(areaTaskStatus);
                    break;
                default:
                    break;
            }
        }
        if (!CollectionUtils.isEmpty(areaTaskStatusList)) {
            distributeStatusDao.modifyAreaTaskStatusByIds(areaTaskStatusList);
        }
    }

    @Override
    public void modifyAreaTaskStatus4ModifyModelNum(Long areaTaskId, Long modelId) {
        AreaModelRel areaModelRel = areaTaskDao.queryAreaModelRelById(areaTaskId, modelId);
        if (null == areaModelRel) {
            return;
        }
        if (MainPushConstants.AREA_TASK_MODEL_STATUS_DISTRIBUTED.equals(areaModelRel.getDistStatus())) {
            distributeStatusDao.modifyAreaTaskRelStatusById(areaModelRel.getId(),
                    MainPushConstants.AREA_TASK_MODEL_STATUS_REDISTRIBUTE);
        }

        AreaTask areaTask = areaTaskDao.queryAreaTaskByAreaTaskId(areaTaskId);
        Short areaStatus = null;
        switch (areaTask.getDistStatus()) {
            // 未分配
            case 0:
                // 不做修改
                break;
            // 已分配
            case 1:
                areaStatus = MainPushConstants.AREA_TASK_STATUS_REDISTRIBUTE;
                break;
            // 重新分配
            case 2:
                // 不做修改
                break;
            // 分配中
            case 3:
                areaStatus = MainPushConstants.AREA_TASK_STATUS_REDISTRIBUTE;
                break;
            default:
                break;
        }

        if (null != areaStatus) {
            distributeStatusDao.modifyAreaTaskStatusById(areaTaskId, areaStatus);
        }

    }

    @Override
    @Transactional
    public void modifyAreaTaskStatus4DistriModelNum(Long areaTaskId) {
        List<AreaModelRel> areaModelRelList = areaTaskDao.queryAreaModelRel(areaTaskId);
        // 已分配数量
        int totalDistributed = 0;

        // 重新分配数量
        int totalReDistributed = 0;

        for (AreaModelRel areaModel : areaModelRelList) {
            if (areaModel.getDistStatus().shortValue() == MainPushConstants.AREA_TASK_MODEL_STATUS_DISTRIBUTED
                    .shortValue()) {
                totalDistributed++;
            }
            if (areaModel.getDistStatus().shortValue() == MainPushConstants.AREA_TASK_MODEL_STATUS_REDISTRIBUTE
                    .shortValue()) {
                totalReDistributed++;
            }
        }

        // 如果有重新分配的，则改为重新分配
        if (totalReDistributed > 0) {
            distributeStatusDao.modifyAreaTaskStatusById(areaTaskId, MainPushConstants.AREA_TASK_STATUS_REDISTRIBUTE);
        }
        // 没有重新分配的，另外考虑
        else {
            // 如果大于等于1个款数且小于总款数是已分配的，表示为分配进行中，更新大区主推任务为分配中状态
            if (totalDistributed >= 1 && totalDistributed < areaModelRelList.size()) {
                distributeStatusDao.modifyAreaTaskStatusById(areaTaskId,
                        MainPushConstants.AREA_TASK_STATUS_DISTRIBUTING);
            }
            // 如果所有的款数都是已分配的，表示为最后一次分配，修改大区主推任务为已分配状态
            else if (totalDistributed == areaModelRelList.size()) {
                distributeStatusDao.modifyAreaTaskStatusById(areaTaskId,
                        MainPushConstants.AREA_TASK_STATUS_DISTRIBUTED);
            }
        }
    }

    @Override
    public void modifyAreaTaskStatus4DeleteModel(Long hqTaskId, Long modelId) {
        // 查询该总部任务下的所有大区任务
        List<AreaTask> areaTaskList = areaTaskDao.queryAreaTaskByHqTaskId(hqTaskId);
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(areaTaskList)) {
            for (AreaTask areaTask : areaTaskList) {
                // 根据大区任务查询任务款式关系
                Long areaTaskId = areaTask.getId();
                List<AreaModelRel> rels = areaTaskDao.queryAreaModelRel(areaTaskId);
                if (org.apache.commons.collections.CollectionUtils.isNotEmpty(rels)) {
                    distributeStatusDao.modifyAreaTaskStatusById(areaTaskId, getAreaStatus(rels));
                    self.calAreaTaskAllStoreDistributeNum(areaTaskId);
                }
            }
        }
    }

    private Short getAreaStatus(List<AreaModelRel> rels) {
        Short status = null;
        Integer isAllUnDist = NumberUtils.INTEGER_ONE; // 统计所有款数是否未分配 默认是
        Integer isAllDist = NumberUtils.INTEGER_ONE; // 统计所有款数是否已分配 默认是
        for (AreaModelRel rel : rels) {
            // 存在款数状态为重新分配 isAllUnDist, isAllDist重置为0 大区任务状态即为重新分配
            if (MainPushConstants.AREA_TASK_MODEL_STATUS_REDISTRIBUTE.equals(rel.getDistStatus())) {
                status = MainPushConstants.AREA_TASK_STATUS_REDISTRIBUTE;
                isAllUnDist = NumberUtils.INTEGER_ZERO;
                isAllDist = NumberUtils.INTEGER_ZERO;
                // 存在款数状态为已分配 isAllUnDist重置为0
            } else if (MainPushConstants.AREA_TASK_STATUS_DISTRIBUTED.equals(rel.getDistStatus())) {
                isAllUnDist = NumberUtils.INTEGER_ZERO;
                // 存在款数状态为未分配 isUnDist重置为0
            } else if (MainPushConstants.AREA_TASK_STATUS_DISTRIBUTED.equals(rel.getDistStatus())) {
                isAllDist = NumberUtils.INTEGER_ZERO;
            }
        }
        if (null != status) {
            return status;
        } else if (NumberUtils.INTEGER_ONE.equals(isAllUnDist)) {
            status = MainPushConstants.AREA_TASK_STATUS_UNDISTRIBUTED;
        } else if (NumberUtils.INTEGER_ONE.equals(isAllDist)) {
            status = MainPushConstants.AREA_TASK_STATUS_DISTRIBUTED;
        } else {
            status = MainPushConstants.AREA_TASK_STATUS_DISTRIBUTING;
        }
        return status;
    }

}
